<?php
require('lib/FLEA/FLEA.php');

// 准备数据库连接信息
$dsn = array(
    'driver'        => 'mysql',
    'host'          => 'localhost',
    'login'         => 'root',
    'password'      => 'gkim',
    'database'      => 'qgzx',
);
// 指定数据库连接设置，TableDataGateway 会自动取出 dbDSN 设置来连接数据库
set_app_inf('dbDSN', $dsn);

// 初始化 FleaPHP 运行环境
FLEA::init();

//// 创建角色名
//FLEA::loadClass('FLEA_Com_RBAC_RolesManager');
//class MyRolesManager extends FLEA_Com_RBAC_RolesManager
//{
//    var $tableName = 'roles';
//    var $primaryKey = 'role_id';
//}
//
//	$rolesManager =& get_singleton('MyRolesManager');
//	/* @var $rolesManager MyRolesManager */
//	$role = array('rolename' => 'SYSTEM_ADMIN');
//	$rolesManager->create($role);
//	$role = array('rolename' => 'POWER_USER');
//	$rolesManager->create($role);
//	$role = array('rolename' => 'MANAGER');
//	$rolesManager->create($role);
//	$role = array('rolename' => 'EDITOR');
//	$rolesManager->create($role);
	
//	//创建用户
//	load_class('FLEA_Com_RBAC_UsersManager');
//	class MyUsersManager extends FLEA_Com_RBAC_UsersManager
//	{
//		var $tableName = 'users';
//		var $primaryKey = 'user_id';
//	}
//	
//	$usersManager =& get_singleton('MyUsersManager');
//	/* @var $usersManager MyUsersManager */
//	
//	$user = array(
//		'username' => 'zero',
//		'password' => 'gkim',
//		'email' => 'jnu.zero@gmail.com',
//	);
//	$usersManager->create($user);

// //设置用户角色
//FLEA::loadClass('FLEA_Com_RBAC_UsersManager');
//FLEA::loadClass('FLEA_Com_RBAC_RolesManager');
//
//class MyRolesManager extends FLEA_Com_RBAC_RolesManager
//{
//    var $tableName = 'roles';
//    var $primaryKey = 'role_id';
//}
//
//class MyUsersManager extends FLEA_Com_RBAC_UsersManager
//{
//    var $tableName = 'users';
//    var $primaryKey = 'user_id';
//
//    var $manyToMany = array(
//        'tableClass' => 'MyRolesManager',
//        'mappingName' => 'roles',
//        'joinTable' => 'roles_users',
//    );
//}
//
//$usersManager =& get_singleton('MyUsersManager');
///* @var $usersManager MyUsersManager */
//
//// 取出用户
//$user = $usersManager->findByUsername('zero');
//// 清空现有的角色信息
//$user[$usersManager->rolesField] = array();
//
//// 取出 POWER_USER 角色
//$rolesManager =& get_singleton('MyRolesManager');
///* @var rolesManager MyRolesManager */
//$role = $rolesManager->find(array('rolename' => 'POWER_USER'));
//// 指定给用户
//$user[$usersManager->rolesField][] = $role[$rolesManager->primaryKey];
//
//// 取出 MANAGER 角色
//$role = $rolesManager->find(array('rolename' => 'MANAGER'));
//// 指定给用户
//$user[$usersManager->rolesField][] = $role[$rolesManager->primaryKey];
//
//// 取出 MANAGER 角色
//$role = $rolesManager->find(array('rolename' => 'EDITOR'));
//// 指定给用户
//$user[$usersManager->rolesField][] = $role[$rolesManager->primaryKey];
//
//// 保存修改后的用户信息
//$usersManager->update($user);
//
//// 重新从数据库读取用户信息，确定为用户指定的角色信息已经保存到数据库
//$user = $usersManager->findByUsername('zero');
//dump($user);


//
//load_class('FLEA_Com_RBAC_RolesManager');
//class MyRolesManager extends FLEA_Com_RBAC_RolesManager
//{
//    var $tableName = 'roles';
//    var $primaryKey = 'role_id';
//}
//
//load_class('FLEA_Com_RBAC_UsersManager');
//class MyUsersManager extends FLEA_Com_RBAC_UsersManager
//{
//    var $tableName = 'users';
//    var $primaryKey = 'user_id';
//
//    var $manyToMany = array(
//        'tableClass' => 'MyRolesManager',
//        'mappingName' => 'roles',
//        'joinTable' => 'roles_users',
//    );
//}
//
///**
// * 模拟登录
// */
//login('zero', 'gkim');
//
///**
// * 处理用户登录
// */
//function login($username, $password) {
//    $usersManager =& get_singleton('MyUsersManager');
//    /* @var $usersManager MyUsersManager */
//
//    // 验证用户名和密码是否正确
//    $user = $usersManager->findByUsername($username);
//    if (!$user || !$usersManager->checkPassword($password, $user[$usersManager->passwordField])) {
//        echo "Username invalid or password mismatch.";
//        exit;
//    }
//
//    // 获取用户角色信息
//    $roles = $usersManager->fetchRoles($user);
//
//    // 获得 FLEA_Com_RBAC 组件实例
//    $rbac =& get_singleton('FLEA_Com_RBAC');
//    /* @var $rbac FLEA_Com_RBAC */
//
//    // 为了降低服务器负担，我们只在 session 中存储用户ID和用户名
//    $sessionUser = array(
//        'USERID' => $user[$usersManager->primaryKey],
//        'USERNAME' => $user[$usersManager->usernameField],
//    );
//
//    // 将用户ID、用户名和角色信息保存到 session
//    $rbac->setUser($sessionUser, $roles);
//
//    // 登录成功
//    echo "Login successed, contents of session:";
//    dump($_SESSION);
//}

?>
